package com.cust.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cust.sky.entity.SetmealDish;
import com.cust.sky.mapper.SetmealDishMapper;
import com.cust.sky.service.SetmealDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐菜品关系 服务实现类
 * </p>
 *
 * @author lei
 * @since 2024-07-10
 */
@Service
public class SetmealDishServiceImpl extends ServiceImpl<SetmealDishMapper, SetmealDish> implements SetmealDishService {

    /**
     * 根据菜品id获取套餐id
     *
     * @param dishId
     * @return
     */
    @Override
    public List<Long> getSetmealIdsByDishId(Long dishId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId, dishId);
        List<Long> setmealIds = baseMapper.selectList(wrapper).stream()
                .map(item -> item.getSetmealId())
                .collect(Collectors.toList());
        return setmealIds;
    }

    /**
     * 根据批量菜品id获取套餐id
     *
     * @param ids
     * @return
     */
    @Override
    public List<Long> getSetmealIdsByDishIds(List<Long> ids) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        List<Long> setmealIds = new ArrayList<>();
        List<SetmealDish> setmealDishList = new ArrayList<>();
        wrapper.in(SetmealDish::getDishId, ids);
        setmealDishList = baseMapper.selectList(wrapper);
        //遍历每个套餐
        for (SetmealDish setmealDish : setmealDishList) {
            //将套餐id添加到setmealIds集合
            setmealIds.add(setmealDish.getSetmealId());
        }
        return setmealIds;
    }

    /**
     * 根据套餐id批量删除
     *
     * @param setmealIds
     */
    @Override
    public void deleteBySetmealIds(List<Long> setmealIds) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, setmealIds);
        baseMapper.delete(wrapper);
    }

    /**
     * 根据套餐id删除
     *
     * @param setmealId
     */
    @Override
    public void deleteBySetmealId(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        baseMapper.delete(wrapper);
    }

    /**
     * 根据套餐id查询套餐菜品表
     *
     * @param setmealId
     * @return
     */
    @Override
    public List<SetmealDish> getBySetmealId(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishes = baseMapper.selectList(wrapper);
        return setmealDishes;
    }

}
